package com.hboxs.asl.wechat.utils;

import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by study on 7/22/2015.
 */
public final class WechatErrorUtils {
    public static final String WXERR_UNIFIED_ORDER_DESCRIBE_KEY = "describe";
    public static final String WXERR_UNIFIED_ORDER_REASON_KEY = "reason";
    public static final String WXERR_UNIFIED_ORDER_SULUTION_KEY = "sulution";

    private WechatErrorUtils() {
    }

    private final static Map<String, Map<String, String>> UNIFIED_ORDER_ERRORS = new HashMap<String, Map<String, String>>();

    {
        Map<String, String> map = new HashMap<String, String>();
        map.put(WXERR_UNIFIED_ORDER_DESCRIBE_KEY, "商户无此接口权限");
        map.put(WXERR_UNIFIED_ORDER_REASON_KEY, "商户未开通此接口权限");
        map.put(WXERR_UNIFIED_ORDER_SULUTION_KEY, "请商户前往申请此接口权限");
        UNIFIED_ORDER_ERRORS.put("NOAUTH", map);
        map = new HashMap<String, String>();
        map.put(WXERR_UNIFIED_ORDER_DESCRIBE_KEY, "余额不足");
        map.put(WXERR_UNIFIED_ORDER_REASON_KEY, "用户帐号余额不足");
        map.put(WXERR_UNIFIED_ORDER_SULUTION_KEY, "用户帐号余额不足，请用户充值或更换支付卡后再支付");
        UNIFIED_ORDER_ERRORS.put("NOTENOUGH", map);
        map = new HashMap<String, String>();
        map.put(WXERR_UNIFIED_ORDER_DESCRIBE_KEY, "商户订单已支付");
        map.put(WXERR_UNIFIED_ORDER_REASON_KEY, "商户订单已支付，无需重复操作");
        map.put(WXERR_UNIFIED_ORDER_SULUTION_KEY, "商户订单已支付，无需更多操作");
        UNIFIED_ORDER_ERRORS.put("ORDERPAID", map);
        map = new HashMap<String, String>();
        map.put(WXERR_UNIFIED_ORDER_DESCRIBE_KEY, "订单已关闭");
        map.put(WXERR_UNIFIED_ORDER_REASON_KEY, "当前订单已关闭，无法支付");
        map.put(WXERR_UNIFIED_ORDER_SULUTION_KEY, "当前订单已关闭，请重新下单");
        UNIFIED_ORDER_ERRORS.put("ORDERCLOSED", map);
        map = new HashMap<String, String>();
        map.put(WXERR_UNIFIED_ORDER_DESCRIBE_KEY, "系统错误");
        map.put(WXERR_UNIFIED_ORDER_REASON_KEY, "系统超时");
        map.put(WXERR_UNIFIED_ORDER_SULUTION_KEY, "系统异常，请用相同参数重新调用");
        UNIFIED_ORDER_ERRORS.put("SYSTEMERROR", map);
        map = new HashMap<String, String>();
        map.put(WXERR_UNIFIED_ORDER_DESCRIBE_KEY, "APPID不存在");
        map.put(WXERR_UNIFIED_ORDER_REASON_KEY, "参数中缺少APPID");
        map.put(WXERR_UNIFIED_ORDER_SULUTION_KEY, "请检查APPID是否正确");
        UNIFIED_ORDER_ERRORS.put("APPID_NOT_EXIST", map);
        map = new HashMap<String, String>();
        map.put(WXERR_UNIFIED_ORDER_DESCRIBE_KEY, "MCHID不存在");
        map.put(WXERR_UNIFIED_ORDER_REASON_KEY, "参数中缺少MCHID");
        map.put(WXERR_UNIFIED_ORDER_SULUTION_KEY, "请检查MCHID是否正确");
        UNIFIED_ORDER_ERRORS.put("MCHID_NOT_EXIST", map);
        map = new HashMap<String, String>();
        map.put(WXERR_UNIFIED_ORDER_DESCRIBE_KEY, "appid和mch_id不匹配");
        map.put(WXERR_UNIFIED_ORDER_REASON_KEY, "appid和mch_id不匹配");
        map.put(WXERR_UNIFIED_ORDER_SULUTION_KEY, "请确认appid和mch_id是否匹配");
        UNIFIED_ORDER_ERRORS.put("APPID_MCHID_NOT_MATCH", map);
        map = new HashMap<String, String>();
        map.put(WXERR_UNIFIED_ORDER_DESCRIBE_KEY, "缺少参数");
        map.put(WXERR_UNIFIED_ORDER_REASON_KEY, "缺少必要的请求参数");
        map.put(WXERR_UNIFIED_ORDER_SULUTION_KEY, "请检查参数是否齐全");
        UNIFIED_ORDER_ERRORS.put("LACK_PARAMS", map);
        map = new HashMap<String, String>();
        map.put(WXERR_UNIFIED_ORDER_DESCRIBE_KEY, "商户订单号重复");
        map.put(WXERR_UNIFIED_ORDER_REASON_KEY, "同一笔交易不能多次提交");
        map.put(WXERR_UNIFIED_ORDER_SULUTION_KEY, "请核实商户订单号是否重复提交");
        UNIFIED_ORDER_ERRORS.put("OUT_TRADE_NO_USED", map);
        map = new HashMap<String, String>();
        map.put(WXERR_UNIFIED_ORDER_DESCRIBE_KEY, "签名错误");
        map.put(WXERR_UNIFIED_ORDER_REASON_KEY, "参数签名结果不正确");
        map.put(WXERR_UNIFIED_ORDER_SULUTION_KEY, "请检查签名参数和方法是否都符合签名算法要求");
        UNIFIED_ORDER_ERRORS.put("SIGNERROR", map);
        map = new HashMap<String, String>();
        map.put(WXERR_UNIFIED_ORDER_DESCRIBE_KEY, "XML格式错误");
        map.put(WXERR_UNIFIED_ORDER_REASON_KEY, "XML格式错误");
        map.put(WXERR_UNIFIED_ORDER_SULUTION_KEY, "请检查XML参数格式是否正确");
        UNIFIED_ORDER_ERRORS.put("XML_FORMAT_ERROR", map);
        map = new HashMap<String, String>();
        map.put(WXERR_UNIFIED_ORDER_DESCRIBE_KEY, "请使用post方法");
        map.put(WXERR_UNIFIED_ORDER_REASON_KEY, "未使用post传递参数?");
        map.put(WXERR_UNIFIED_ORDER_SULUTION_KEY, "请检查请求参数是否通过post方法提交");
        UNIFIED_ORDER_ERRORS.put("REQUIRE_POST_METHOD", map);
        map = new HashMap<String, String>();
        map.put(WXERR_UNIFIED_ORDER_DESCRIBE_KEY, "post数据为空");
        map.put(WXERR_UNIFIED_ORDER_REASON_KEY, "post数据不能为空");
        map.put(WXERR_UNIFIED_ORDER_SULUTION_KEY, "请检查post数据是否为空");
        UNIFIED_ORDER_ERRORS.put("POST_DATA_EMPTY", map);
        map = new HashMap<String, String>();
        map.put(WXERR_UNIFIED_ORDER_DESCRIBE_KEY, "编码格式错误");
        map.put(WXERR_UNIFIED_ORDER_REASON_KEY, "未使用指定编码格式");
        map.put(WXERR_UNIFIED_ORDER_SULUTION_KEY, "请使用NOT_UTF8编码格式");
        UNIFIED_ORDER_ERRORS.put("NOT_UTF8", map);
    }


    public static String unifiedOrderErrorDescribe(String key) {
        String rst = "";
        if (!StringUtils.isEmpty(key)) {
            Map<String, String> map = UNIFIED_ORDER_ERRORS.get(key);
            if (map != null) rst = map.get(WXERR_UNIFIED_ORDER_DESCRIBE_KEY);
        }

        return rst;
    }

    public static String unifiedOrderErrorReason(String key) {
        String rst = "";
        if (!StringUtils.isEmpty(key)) {
            Map<String, String> map = UNIFIED_ORDER_ERRORS.get(key);
            if (map != null) rst = map.get(WXERR_UNIFIED_ORDER_REASON_KEY);
        }

        return rst;
    }

    public static String unifiedOrderErrorSulution(String key) {
        String rst = "";
        if (!StringUtils.isEmpty(key)) {
            Map<String, String> map = UNIFIED_ORDER_ERRORS.get(key);
            if (map != null) rst = map.get(WXERR_UNIFIED_ORDER_SULUTION_KEY);
        }

        return rst;
    }

    public static Map<String, String> unifiedorderError(String key) {
        Map<String, String> rst = null;
        if (!StringUtils.isEmpty(key)) {
            Map<String, String> map = UNIFIED_ORDER_ERRORS.get(key);
            if (map != null) rst = map;
        }

        return rst;
    }
}
